﻿using System;

namespace SAP.BusinessOne.WebBridge.Framework
{
    static class InteropUtil
    {
        public static object[] EncapsulateParameterList(Args args, TypeCode[] ip)
        {
            var objs = new object[ip.Length];
            for (var i = 0; i < ip.Length; i++)
            {
                object obj = null;
                switch (ip[i])
                {
                    case TypeCode.Boolean:
                        {
                            Boolean data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.Byte:
                        {
                            Byte data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.Char:
                        {
                            Char data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.Double:
                        {
                            Double data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.Int16:
                        {
                            Int16 data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.Int32:
                        {
                            Int32 data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.Int64:
                        {
                            Int64 data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.SByte:
                        {
                            SByte data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.Single:
                        {
                            Single data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.UInt16:
                        {
                            UInt16 data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.UInt32:
                        {
                            UInt32 data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.UInt64:
                        {
                            UInt64 data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.String:
                        {
                            String data;
                            args.Get(out data);
                            obj = data;
                            break;
                        }
                    case TypeCode.Empty:
                    case TypeCode.DBNull:
                    case TypeCode.DateTime:
                    case TypeCode.Decimal:
                    case TypeCode.Object:
                    default:
                        throw new NotSupportedException("Unsupported return type");
                }
                objs[i] = obj;
            }
            return objs;
        }

        public static void PutReturnData(Args args, TypeCode op, object data)
        {
            switch (op)
            {
                case TypeCode.Boolean:
                    args.Put((Boolean)data);
                    break;
                case TypeCode.Byte:
                    args.Put((Byte)data);
                    break;
                case TypeCode.Char:
                    args.Put((Char)data);
                    break;
                case TypeCode.Double:
                    args.Put((Double)data);
                    break;
                case TypeCode.Int16:
                    args.Put((Int16)data);
                    break;
                case TypeCode.Int32:
                    args.Put((Int32)data);
                    break;
                case TypeCode.Int64:
                    args.Put((Int64)data);
                    break;
                case TypeCode.SByte:
                    args.Put((SByte)data);
                    break;
                case TypeCode.Single:
                    args.Put((Single)data);
                    break;
                case TypeCode.UInt16:
                    args.Put((UInt16)data);
                    break;
                case TypeCode.UInt32:
                    args.Put((UInt32)data);
                    break;
                case TypeCode.UInt64:
                    args.Put((UInt64)data);
                    break;
                case TypeCode.String:
                    args.PutString((String)data);
                    break;
                case TypeCode.Empty:
                    // void type return nothing
                    break;
                case TypeCode.DBNull:
                case TypeCode.DateTime:
                case TypeCode.Decimal:
                case TypeCode.Object:
                default:
                    throw new NotSupportedException("Unsupported return type");
            }
        }
    }
}
